<template>
  <div class="page-wrapper">

    <el-card>
      <el-row style="margin-bottom: 20px;" type="flex" justify="end">
        <el-button size="small" type="primary" @click="addDialog(1,'0')">添加权限</el-button>
      </el-row>
      <el-table :data="list" border row-key="id" default-expand-all>
        <!-- :tree-props="{children: 'roles', hasChildren: 'hasChildren'}" -->
        <el-table-column label="权限名称" prop="title" />
        <el-table-column label="权限标识" prop="code" />
        <el-table-column label="权限描述" prop="description" />
        <el-table-column>
          //v-if判断type为1时,为子权限的添加
          <template v-slot="{row}">
            <el-button v-if="row.type==='1'" size="small" type="primary" @click="addDialog(2, row.id)">添加子权限</el-button>
            <!-- ? row.id : '0' -->
            <el-button size="small" type="warning" @click="showEditDialog(row.id)">编辑权限</el-button>
            <el-button size="small" type="danger" @click="delPerm(row.id)">删除权限</el-button>
          </template>
        </el-table-column>
      </el-table>
      <!-- 分页/翻页器 -->
      <!-- total 指定总条数
        page-size 指定每页长度, 自动得出页码 -->
      <!--  <el-row type="flex" justify="center" style="margin-top: 20px">
        <el-pagination
          :total="total"
          :page-size="pageConfig._limit"
          :current-page.sync="page"
          @current-change="currentChange"
        />
      </el-row> -->

    </el-card>

    <el-dialog :title="formData.id?`编辑权限`:'新增权限'" :visible="isShowDialog" @close="isCancel">
      <!-- 表单 -->
      <el-form ref="addForm" :model="formData" :rules="rules" label-width="120px">
        <el-form-item label="权限名称" prop="title">
          <el-input v-model="formData.title" style="width:90%" />
        </el-form-item>
        <el-form-item label="权限标识" prop="code">
          <el-input v-model="formData.code" style="width:90%" />
        </el-form-item>
        <el-form-item label="权限描述">
          <el-input v-model="formData.description" style="width:90%" />
        </el-form-item>
        <el-form-item label="开启">
          <el-switch
            v-model="formData.enVisible"
            active-value="1"
            inactive-value="0"
          />
        </el-form-item>
      </el-form>
      <el-row slot="footer" type="flex" justify="center">
        <el-col :span="6">
          <el-button size="small" type="primary" @click="isOk">确定</el-button>
          <el-button size="small" @click="isCancel">取消</el-button>
        </el-col>
      </el-row>
    </el-dialog>
  </div>
</template>

<script>
import { listToTree } from '@/utils'
import { getPermission, getOnlyPermission, addPermission, editPermission, deletePermission, getPermissionCount } from '@/api/permission'
export default {
  data() {
    return {
      // 角色列表总条数
      total: 0,
      // 分页配置
      // 当前页码
      page: 1,
      pageConfig: {
        // 每页条数
        _limit: 6,
        // 开始获取的位置
        _start: 0
      },
      list: [],
      isShowDialog: false,
      formData: {
        title: '',
        code: '',
        description: '',
        pid: ''

      },
      rules: {
        title: [
          { required: true, message: '输入不能为空', trigger: 'blur' }
        ],
        code: [
          { required: true, message: '输入不能为空', trigger: 'blur' }
        ],
        description: [
          { required: true, message: '输入不能为空', trigger: 'blur' }
        ]

      }

    }
  },
  created() {
    this.getPermission()

    this.getPermissionCount()
  },
  methods: {
    // 获取角色权限列表
    async getPermission() {
      // this.pageConfig._start = (this.page - 1) * this.pageConfig._limit
      // this.pageConfig
      const res = await getPermission()
      this.list = res.data
      // console.log('1', res)
      const t = listToTree(res.data, '0')
      // console.log('2222', t)
      this.list = t
    },
    currentChange(currentPage) {
      // console.log(`当前页: ${currentPage}`)

      this.page = currentPage
      this.getPermission()
    },
    addDialog(type, pid) {
      this.formData.type = type
      this.formData.pid = pid
      this.isShowDialog = true
    },
    // 获取角色数量
    async getPermissionCount() {
      const res = await getPermissionCount()
      // console.log('222', total)
      this.total = res.data
    },
    // 点击编辑按钮,回显数据
    async showEditDialog(id) {
      const res = await getOnlyPermission(id)
      this.formData = res.data
      this.id = id
      this.isShowDialog = true
      // console.log('formData', this.formData)
    },
    // 点击确定按钮,判断时添加还是编辑
    async isOk() {
      // 检验表单
      await this.$refs.addForm.validate()
      if (!this.formData.id) {
        // 添加
        // console.log('formData', this.formData)
        this.formData.pid = String(this.formData.pid)
        this.formData.type = String(this.formData.type)
        // this.formData.type = '2'
        // console.log(this.formData)
        await addPermission(this.formData)
        /*  this.total++
        const lastPage = Math.ceil(this.total / this.pageConfig._limit)
        this.page = lastPage */
      } else {
        // 编辑
        await editPermission(this.formData)
      }

      // 操作成功
      this.$message.success('操作成功')
      this.getPermission()
      this.isShowDialog = false
    },
    async isCancel() {
      await this.$refs.addForm.resetFields()
      setTimeout(() => {
        this.formData = {
          title: '',
          description: ''
        }
      }, 500)
      this.isShowDialog = false
    },
    // 删除权限
    async delPerm(id) {
      // 二次询问
      await this.$confirm('确定删除吗？')
      // 发送请求
      await deletePermission(id)
      // 提示
      this.$message.success('删除成功')
      /*   if (this.list.length === 1 && this.page !== 1) {
        this.page--
      } */
      // 刷新页面
      this.getPermission()
    }

  }

}
</script>

<style lang="scss" scoped>
 .text {
    font-size: 14px;
  }

  .item {
    margin-bottom: 18px;
  }

  .clearfix:before,
  .clearfix:after {
    display: table;
    content: "";
  }
  .clearfix:after {
    clear: both
  }

</style>
